package com.tjtt.tdm.system.action;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.CronTriggerBean;

import com.tjtt.common.utils.StringUtil;
import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.system.bean.ConfigInfo;
import com.tjtt.tdm.system.service.ConfigService;
/**
 * 系统设定管理
 * 
 * @author 曹晓芳
 * 
 */
public class ConfigAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(ConfigAction.class);
	private Scheduler scheduler;  
	private ConfigService configService;
	private ConfigInfo info;
	
	public ConfigInfo getInfo() {
		return info;
	}

	public void setInfo(ConfigInfo info) {
		this.info = info;
	}

	/**
	 * 查询系统配置信息方法
	 * @return
	 * @throws IOException
	 * @throws ServiceException String
	 * @author 曹晓芳
	 * @date Aug 30, 2012 11:30:03 AM
	 */
	public String searchAllSysCfgExec() throws IOException, ServiceException {	
		setContentType();
		info = this.configService.getConfigInfoById(null);
		return "configInfo";		
	}
	
	/**
	 * 添加修改系统配置项方法
	 * @return
	 * @throws IOException
	 * @throws ServiceException String
	 * @author 曹晓芳
	 * @throws SchedulerException 
	 * @throws ParseException 
	 * @date Aug 30, 2012 11:30:03 AM
	 */
	public String saveOrUpdateExec() throws IOException, ServiceException, SchedulerException, ParseException {	
		try {
			setContentType();
			info.setSysEdittime(new Date());
			info.setSysEdituserid(this.getUser().getUserid());
			info.setSysOrgid(this.getUser().getOrganization().getOrgId());
			String logDeltime = info.getSysDeltime();
			if(!StringUtil.isEmpty(logDeltime)){
				// 运行时可通过动态注入的scheduler得到trigger
//				CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger(
//						"doTime", Scheduler.DEFAULT_GROUP);
//		
//				//String originConExpression = trigger.getCronExpression();
//				// 把定时器的执行时间间隔改为每2分钟执行一次
//				trigger.setCronExpression("0 0 "+logDeltime+" * * ?");
//				scheduler.rescheduleJob("cronTrigger", Scheduler.DEFAULT_GROUP, trigger);
			}
			this.configService.saveOrUpdateConfigInfo(info);
			getRequest().setAttribute("message", "修改成功！");
			return "configInfo";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public void setConfigService(ConfigService configService) {
		this.configService = configService;
	}

	public Scheduler getScheduler() {
		return scheduler;
	}

	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}
	
}
